/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.model;


/**
 * Base class for all terms. 
 * 
 * @author Krzysztof Langner
 *
 */
public class Term {

	private String	text;
	private boolean negative = false;
	
	
	/**
	 * Konstruktor
	 */
	protected Term(String text){
		this.text = text;
	}
	
	
	/**
	 * @return tekst wyrażenia
	 */
	public Term copy(){
		
		// nie można utworzyć obiektu tego typu
		assert(false);
		return null;
	}


	/**
	 * Sprawdzenie czy 2 wyrazenia sa sobie równowazne w sensie matematycznym.
	 */
	public boolean equals(Object obj){

		return (toString().compareTo(obj.toString()) == 0);
	}
	

	/**
	 * Sprawdzenie czy 2 wyrazenia sa sobie równowazne w sensie matematycznym.
	 */
	public boolean equalsTo(Term term){

		return (isAbsTheSame(term) && term.isNegative() == isNegative());
	}
	

	/**
	 * Try to evaluate expression
	 */
	public Term evaluate(){
		return this;
	}
	

	/**
	 * @return tekst wyrażenia
	 */
	public String getText(){
		return text;
	}

	
	/**
	 * Return hashcode
	 */
	public int hashCode(){
		return toString().hashCode();
	}

	
	/**
	 * Pobranie minusa przed wyrazeniem
	 */
	public boolean isNegative() {
		return negative;
	}
	
	
	/**
	 * Zwraca true jeżeli wyrażenie jest liczbą.
	 * Liczbą jest zarówno 2 jaki i 2+3, 2^2, 2/3$2
	 */
	public boolean isNumber() {
		return false;
	}
	
	
	/**
	 * Zwraca true jeżeli wyrażenie jest liczbą rzeczywistą
	 * może to być: 2, 2+3, 2^2, 2/3$2
	 * ale nie (-2)$2
	 */
	public boolean isRealNumber() {
		return !evaluate().getText().contains("NaN");
	}
	
	
	/**
	 * Sprawdzenie czy 2 wartości bezwgledne wyrazenia sa sobie równowazne.
	 */
	public boolean isAbsTheSame(Term term){
		return false;
	}
	

	/**
	 * Uporzadkowanie minusow.
	 */
	public void normalizeSign(){
		
	}
	
	
	/**
	 * sortuje wyrazenia
	 * @param b
	 */
	public void sort() {
	}

	
	/**
	 * Ustawienie znaku minus w wyrazeniu
	 * @param b
	 */
	public void setNegative(boolean b) {
		negative = b;
	}
	
	  /**
     * Convert object to string
     */
    public String toString(){
		
		String output = "";
		
		if(negative)
			output = "-";
		
		output += text;
		return output;
	}


	/**
	 * Transformacja drzewa
	 * @param transform
	 * @return
	 */
	public Term transform(ITransform t) {
		return t.process(this);
	}

}
